package com.grace.cms.forum.user.controller.hand;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.grace.cms.common.enums.RecordTypeEnums;
import com.grace.cms.forum.comment.domain.CmsComment;
import com.grace.cms.forum.comment.domain.CmsCommentHand;
import com.grace.cms.forum.comment.domain.enums.CommentHandVisibleEnums;
import com.grace.cms.forum.comment.domain.enums.CommentVisibleEnums;
import com.grace.cms.forum.comment.domain.properties.OlyCommentProperties;
import com.grace.cms.forum.comment.domain.vo.CmsCommentVo;
import com.grace.cms.forum.comment.service.impl.CmsCommentHandServiceImpl;
import com.grace.cms.forum.comment.service.impl.CmsCommentServiceImpl;
import com.grace.cms.forum.user.service.RecordStatusService;
import com.grace.cms.forum.user.service.tadlib.CommentTag;
import com.grace.cms.general.controller.CommonController;
import com.grace.common.annotation.RepeatSubmit;
import com.grace.common.core.domain.AjaxResult;
import com.grace.common.core.text.Convert;
import com.grace.common.utils.ServletUtils;
import com.grace.common.utils.html.EscapeUtil;
import com.grace.common.utils.ip.IpUtils;
import com.grace.system.service.impl.SysSearchConfigServiceImpl;
import com.grace.system.utils.SecurityUtils;

import eu.bitwalker.useragentutils.UserAgent;

/**
 * 评论控制
 * 
 * 小于默认一分钟重复提交
 */
@RestController
@RequestMapping("/{themeName}/hand/comment")
public class CommentHandController extends CommonController {

    @Autowired
    private CmsCommentServiceImpl cmsCommentService;

    @Autowired
    private CmsCommentHandServiceImpl cmsCommentHandService;

    @Autowired
    private SysSearchConfigServiceImpl configService;

    @Autowired
    private CommentTag commentTag;

    @Autowired
    private RecordStatusService recordStatusService;

    /**
     * 添加评论
     * 
     * @param cmsComment
     * @return
     */
    @RepeatSubmit(interval = 60000)
    @PostMapping("/{type}/addComment")
    public AjaxResult addComment(@PathVariable("themeName") String themeName,@RequestBody @Validated CmsComment cmsComment,
            @PathVariable("type") RecordTypeEnums type) {
        String maxSize = configService.selectConfigDefaultValue(
                OlyCommentProperties.COMMENT_CONFIG_GROUP.defaultValue(),
                OlyCommentProperties.COMMENT_MAX_SIZE);
        if (Integer.parseInt(maxSize) < EscapeUtil.clean(cmsComment.getContent()).length()) {
            return AjaxResult.error("内容超出大上限:" + maxSize + "个字符");
        }
        if (commentTag.commentSupport(type, themeName)) {
            switch (type) {
                case ARTICLE:
                    if (!recordStatusService.selectArticleCommentStatus(Convert.toLong(cmsComment.getRecordId()))) {
                        return AjaxResult.error("评论已关闭.");
                    }
                    break;
                case CATEGORY:

                    break;
                case LINK:

                    break;
                default:
                    return AjaxResult.error("评论类型不能为空!");
            }
            if (SecurityUtils.getLoginUser() == null) {
                return AjaxResult.error("请登录后进行操作.");
            } else if (SecurityUtils.getUsername().equals(cmsComment.getReplyBy())) {
                return AjaxResult.error("不允许回复自己.");
            } else {
                setCommonRecordParam(cmsComment, themeName, type,false);
                UserAgent userAgent = UserAgent.parseUserAgentString(ServletUtils.getRequest().getHeader("User-Agent"));
                String ip = IpUtils.getIpAddr();
                cmsComment.setIp(ip);
                cmsComment.setUserSystem(userAgent.getOperatingSystem().getName());
                cmsComment.setUserBower(userAgent.getBrowser().getName());
                String value = configService.selectConfigDefaultValue(
                        OlyCommentProperties.COMMENT_CONFIG_GROUP.defaultValue(),
                        OlyCommentProperties.COMMENT_DEFAULT_VISIBLE);
                cmsComment.setVisible(CommentVisibleEnums.valueOf(value).ordinal());
                return AjaxResult.success(cmsCommentService.insertCmsComment(cmsComment));
            }
        } else {
            return AjaxResult.error("全站禁止评论!");
        }
    }

    /**
     * 点赞|取消点赞
     * 
     * @param commentId
     * @return
     */
    @RepeatSubmit(interval = 60000)
    @PostMapping("/addCommentLike")
    public AjaxResult addCommentLike(@PathVariable("themeName") String themeName, long commentId) {
        CmsComment comment = cmsCommentService.selectCmsCommentVoById(commentId);
        if (comment == null || comment.getVisible().intValue() != CommentVisibleEnums.PASS.ordinal()) {
            return AjaxResult.error("操作节点不存在或者不存在");
        }
        CmsCommentHand aHand = cmsCommentHandService.selectCmsCommentHand(
                commentId, SecurityUtils.getUsername());
        if (aHand == null) {
            aHand = new CmsCommentHand();
            aHand.setCommentId(commentId);
            aHand.setRecordId(comment.getRecordId());
            aHand.setCreateBy(SecurityUtils.getUsername());
            aHand.setVisible(CommentHandVisibleEnums.LIKE.ordinal());
            return AjaxResult.success(cmsCommentHandService.insertCmsCommentHand(aHand));
        } else {
            int vvl = aHand.getVisible().intValue();
            if (vvl == CommentHandVisibleEnums.LIKE.ordinal()) {
                aHand.setVisible(CommentHandVisibleEnums.C_LIKE.ordinal());
            } else if (vvl == CommentHandVisibleEnums.NASTY.ordinal()) {
                return AjaxResult.error("不允许同时赞和踩");
            } else {
                aHand.setVisible(CommentHandVisibleEnums.LIKE.ordinal());
            }
            cmsCommentHandService.updateCmsCommentHand(aHand);
            return AjaxResult.success(aHand);
        }
    }

    /**
     * 反对|取消反对
     * 
     * @param commentId
     * @return
     */
    @RepeatSubmit(interval = 60000)
    @PostMapping("/addCommentNasty")
    public AjaxResult addCommentNasty(@PathVariable("themeName") String themeName, long commentId) {
        CmsCommentVo comment = cmsCommentService.selectCmsCommentVoById(commentId);
        if (comment == null || comment.getVisible().intValue() != CommentVisibleEnums.PASS.ordinal()) {
            return AjaxResult.error("操作节点不存在或者不存在");
        }
        CmsCommentHand aHand = cmsCommentHandService.selectCmsCommentHand(
                commentId, SecurityUtils.getUsername());
        if (aHand == null) {
            aHand = new CmsCommentHand();
            aHand.setCommentId(commentId);
            aHand.setRecordId(comment.getRecordId());
            aHand.setCreateBy(SecurityUtils.getUsername());
            aHand.setVisible(CommentHandVisibleEnums.NASTY.ordinal());
            return AjaxResult.success(cmsCommentHandService.insertCmsCommentHand(aHand));
        } else {
            int vvl = aHand.getVisible().intValue();
            if (vvl == CommentHandVisibleEnums.NASTY.ordinal()) {
                aHand.setVisible(CommentHandVisibleEnums.C_NASTY.ordinal());
            } else if (vvl == CommentHandVisibleEnums.LIKE.ordinal()) {
                return AjaxResult.error("不允许同时赞和踩");
            } else {
                aHand.setVisible(CommentHandVisibleEnums.NASTY.ordinal());
            }
            return AjaxResult.success(cmsCommentHandService.updateCmsCommentHand(aHand));
        }
    }

    @PostMapping("/selectCommentHand")
    public AjaxResult selectCommentHand(@PathVariable("themeName") String themeName, long commentId) {
        CmsCommentHand aHand = cmsCommentHandService.selectCmsCommentHand(
                commentId, SecurityUtils.getUsername());
        return AjaxResult.success(aHand);
    }

    @GetMapping("/listCommentHandByRecordId/{recordId}")
    public AjaxResult listCommentHand(@PathVariable("recordId") String recordId) {
        CmsCommentHand cmsCommentHand = new CmsCommentHand();
        cmsCommentHand.setRecordId(recordId);
        cmsCommentHand.setCreateBy(SecurityUtils.getUsername());
        return AjaxResult.success(cmsCommentHandService.listCmsCommentHand(cmsCommentHand));
    }

}
